package com.frogs.ucenter.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.service.AbsService;

public class UserBaseServiceImpl extends AbsService {
	
	private Logger log = LoggerFactory.getLogger(UserBaseServiceImpl.class);
	
	
	/**
	 * 获取地区信息
	 * @param province
	 * @param city
	 * @param district
	 * @return
	 */
	public JSONObject getAreaInfo(String province,String city, String district) throws Exception {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("AREA_INFO_SHOW");
		String[] otherParams = {ApiUrlParamEnum.province.getParamName(),province,
				ApiUrlParamEnum.city.getParamName(),city,
				ApiUrlParamEnum.district.getParamName(),district};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result.getJSONObject("response").getJSONObject("info");
		} catch (IllegalStateException e) {
			log.error("func[getAreaInfo] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("获取地区信息接口异常！");
		}
		return null;
	}
	
	/**
	 * 查询用户送货地址
	 * @param pageNo
	 * @param pageSize
	 * @param userId
	 * @return
	 */
	public JSONObject getUserAddressList(Integer pageNo, Integer pageSize, Integer userId) throws Exception {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_ADDRESS_LIST");
		String[] otherParams = {ApiUrlParamEnum.pageNo.getParamName(),pageNo.toString(),
								ApiUrlParamEnum.pageSize.getParamName(),pageSize.toString(),
								ApiUrlParamEnum.userId.getParamName(),userId.toString(),
								};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (null == result || !result.containsKey("response") 
					|| ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[getUserAddressList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("查询用户送货地址接口异常！");
		}
		return result;
	}
	
	/**
	 * 获取用户基础信息
	 * @param userId
	 * @return
	 */
	public JSONObject getUserBaseInfoForJson(Integer userId) throws Exception {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_BASE_INFO_SHOW");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(),userId.toString(),
								};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (IllegalStateException e) {
			log.error("func[getUserBaseInfoForJson] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("获取用户基础信息接口异常！");
		}
		return result;
	}
	
	
}
